package com.QK.cnstudy;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.QK.cnstudy.db.DBManager;
import com.QK.cnstudy.entity.Sentence;
import com.QK.cnstudy.entity.Work;

public class TranslateActivity extends Activity implements OnClickListener {
    private int workId;
    private TextView titleTextView;
    private TextView contentTextView;
    private DBManager dbManager;
    private String titleString;
    private String contentString;

    Button backButton;
    Button introductionButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.translate);
        getView();
        Bundle bundle = getIntent().getExtras();
        dbManager = CnStudyApp.app.dbManager;
        workId = bundle.getInt("id");
        Work work = dbManager.getWorkById(workId);
        titleString = work.getName();

        contentString = "";
        List<Sentence> sentences = dbManager.getSentences(workId);
        for (Sentence sentence : sentences) {
            contentString += sentence.getTranslation();
        }
        titleTextView.setText(titleString);

//        contentString += "<br/><br/><br/>";
        contentString = fixFormat(contentString);
        contentTextView.setText(Html.fromHtml(contentString));

        backButton.setOnClickListener(this);
        introductionButton.setOnClickListener(this);
    }

    public static String fixFormat(String content) {

        String head = "　　";
        content = head + content;
        content = content.replaceAll("\\n ", "<br/>" + head);
        content = content.replaceAll("\\n", "<br/>" + head);
        return content;
//        return content.substring(0, content.length() - head.length());
    }

    public void getView() {
        titleTextView = (TextView) findViewById(R.id.state);
        contentTextView = (TextView) findViewById(R.id.content);
        introductionButton = (Button) findViewById(R.id.introduction);
        backButton = (Button) findViewById(R.id.back);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.back:
            onBackPressed();
            break;
        case R.id.introduction:
            Intent intent = new Intent(this, AuthorIntroductionActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Bundle bundle = new Bundle();
            Work work = dbManager.getWorkById(workId);
            bundle.putInt("id", work.getAuthorId());
            intent.putExtras(bundle);
            startActivity(intent);
        default:
            break;
        }

    }
}
